/*
 * Copyright (c) 2009-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_PIXEL_FORMAT_H_INCLUDED_
#define ALKES_PIXEL_FORMAT_H_INCLUDED_

#include "alkes/core/Types.h"

namespace alkes {

/// \addtogroup media
//@{

typedef int32_t PixelFormat;

#define AL_PIXEL_FORMAT_UNKNOWN         (0)
#define AL_PIXEL_FORMAT_RGBA8888        (1)
#define AL_PIXEL_FORMAT_RGBX8888        (2)
#define AL_PIXEL_FORMAT_RGB888          (3)
#define AL_PIXEL_FORMAT_RGBA4444        (4)
#define AL_PIXEL_FORMAT_RGBX4444        (5)
#define AL_PIXEL_FORMAT_RGB565          (6)
#define AL_PIXEL_FORMAT_A8              (7)

#define AL_EXT_FORMAT(idx)              ((idx) + 20)
#define AL_PIXEL_FORMAT_EXT_BGRA8888    (AL_EXT_FORMAT(0))
#define AL_PIXEL_FORMAT_EXT_BGR888      (AL_EXT_FORMAT(1))


#define _al_fmt_make_comp_format(idx)  (-(idx))

#define AL_FORMAT_PNG       _al_fmt_make_comp_format(1)
#define AL_FORMAT_JPEG      _al_fmt_make_comp_format(2)
#define AL_FORMAT_BITMAP    _al_fmt_make_comp_format(3)


struct AL_EXPORT PixelFormatTraits
{
    uint8_t bytes;
    uint8_t component_num;
    int8_t alpha_index;
    struct ComponentInfo
    {
        int8_t offset;
        uint8_t bits;
    } components[4];

    const Color (*getPixel)(const uint8_t* p);
    void (*setPixel)(uint8_t* p, const Color& color);
    void (*fillScanline)(uint8_t* p, uint16_t num, const Color& color);
};

//@}

typedef const PixelFormatTraits* (*GetPixelFormatTraits)(PixelFormat format);

}

AL_EXPORT_API const alkes::PixelFormatTraits* alkes_get_pixel_format_traits(alkes::PixelFormat format);
AL_EXPORT_API alkes::GetPixelFormatTraits alkes_set_pixel_format_traits_func(alkes::GetPixelFormatTraits function);

#define alkes_get_bytelength(format, w) (alkes_get_pixel_format_traits(format)->bytes * w)
#define alkes_get_byteperpixel(format)  (alkes_get_pixel_format_traits(format)->bytes)

#endif
